# @shuaige : 陈世玉
# @name :3-使用re解析豆瓣电影.py
# @time :2025/1/16 18:03
# 1.先拿到源代码
# 使用re进行解析数据
import requests
import re
import csv  #存储文件
def get_html(url) :
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
    }
    response = requests.get(url,headers=header)
    return response.text
def parse_html(html) :
    # <span class="title">肖申克的救赎</span>
    pattern=re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)'
                       r'</span>.*?<p class="">.*?<br>(?P<year>.*?)&nbsp.*?'
                       r'<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?'
                       r'<span>(?P<people>.*?)</span>',re.S)  # re.S表示匹配包括换行符在内的所有字符
    result=pattern.finditer(html)

    return result

if __name__ == '__main__':

    for page in range(10):
        if page==1 :
            url = 'https://movie.douban.com/top250'
        else :
            url = 'https://movie.douban.com/top250?start='+str(25*page-25)
            html = get_html(url)
            with open('douban.csv', 'a', encoding='utf-8', newline='') as f:
                f.write('name,year,score,people\n')
                for e in parse_html(html):
                    print(e.group('name'),e.group('year').strip(),e.group('score'),e.group('people'))
                    f.write(e.group('name')+','+e.group('year').strip()+','+e.group('score')+','+e.group('people')+'\n')


